package megadrum.gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JSpinner;
import megadrum.Common;

public class CurveGraph extends Canvas implements MouseListener, MouseMotionListener {

    public CurveGraph(JSpinner spinner[], int curveNumber) {
        this.spinner = spinner;
        value = new int[9];
        for (int i = 0; i < 9; i++) {
            value[i] = Common.CURVE_PRESET[curveNumber][i];
        }
        setBackground(Color.WHITE);
        image = new ImageIcon(getClass().getResource("/megadrum/icons/curveGraph.png")).getImage();

        //add event listeners for dragging points
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        //canvas size should be 300x300 for this paint method to work nice
        super.paint(g);
        g.drawImage(image, 0, 0, this);

        g.setColor(Color.BLUE);
        for (int i = 0; i < value.length - 1; i++) {
            g.drawLine(35 + 32*i, 266 - value[i], 35 + 32*(i + 1), 266 - value[i + 1]);
        }

        g.setColor(Color.RED);
        for (int i = 0; i < value.length; i++) {
            g.fillOval(35 + 32*i - 2, 266 - value[i] - 2, 5, 5);
        }
    }

    public int[] getPoints(){
        return value;
    }
    public int getPoint(int point){
        return value[point];
    }

    public void setPoint(int point, int val) {
        //apply limits
        if (val < 2) {
            val = 2;
        }
        if (val > 255) {
            val = 255;
        }
        //update value in array
        value[point] = val;
        //and in spinner
        ((CustomSpinnerModel)spinner[point].getModel()).setIntValue(val);
        //update graphics
        repaint();
    }

    public void mousePressed(MouseEvent e) {
        int i = 0;
        for (; i < value.length; i++) {
            if (e.getX() - 10 < 35 + 32*i &&
                    35 + 32*i < e.getX() + 10 &&
                    e.getY() - 10 < 266 - value[i] &&
                    266 - value[i] < e.getY() + 10) {
                break;
            }
        }
        //if some point has been clicked, start dragging mode
        if (i != value.length) {
            dragging = i; //specify which point has been clicked
        }
    }
    
    public void mouseDragged(MouseEvent e) {
        if (dragging != -1) { //if we are dragging some point...
            setPoint(dragging, 266 - e.getY());
        }
    }
    
    public void mouseReleased(MouseEvent e) {
        dragging = -1; //stop dragging
    }

    public void mouseMoved(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }


    //VARIABLES
    private JSpinner[] spinner; //spinners of the EditCustomCurves dialog, we'll update them if we move a point
    private int[] value; //current value of each point
    private int dragging = -1; //specify the index of which point is being dragged
    private Image image;
    private int[] offset = {
        0, 32, 64, 96, 128, 160, 192, 224, 254
    };

}
